<template>
  <div ref="chart" :style="{ width: '100%', height: '200px' }"></div>
</template>


<script lang="ts" setup>
import * as echarts from 'echarts'
import {  ref,  onMounted, onBeforeUnmount} from "vue";
const chart = ref(null)

const option = ref({
  tooltip: {
    trigger: 'item'
  },
  // legend: {
  //   top: '5%',
  //   left: 'center'
  // },
  series: [
    {
      name: 'Access From',
      type: 'pie',
      radius: ['40%', '70%'],
      avoidLabelOverlap: false,
      itemStyle: {
        borderRadius: 10,
        borderColor: '#fff',
        borderWidth: 2
      },
      label: {
        show: false,
        position: 'center'
      },
      emphasis: {
        label: {
          show: true,
          fontSize: '10',
        }
      },
      labelLine: {
        show: false
      },
      data: [
        { value: 1048, name: '音乐' },
        { value: 735, name: '书籍' },
        { value: 580, name: '跑步' },
        { value: 484, name: '篮球' },
        { value: 300, name: '游泳' }
      ]
    }
  ]
})

let myChart:any 

const initChart = () => {
  myChart.resize()
}

onMounted(() => {
  // mounted中初始化charts
  myChart = echarts.init(chart.value as unknown as HTMLElement)
  myChart.setOption(option.value)
  // resize事件
  window.addEventListener("resize", initChart)
})


onBeforeUnmount(() => {
  // 销毁resize事件
  window.removeEventListener('resize', initChart)
})
</script>